package com.gem.growingtree.controller;

import java.util.List;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gem.growingtree.model.CampusInformation;
import com.gem.growingtree.service.CampusInformationService;


@SuppressWarnings("serial")
@Controller
public class CampusInformationController extends HttpServlet{
	@RequestMapping("/campusInformation")
	public ModelAndView addInformation(HttpServletRequest request,HttpSession session,String campusInformationTitle,String campusInformationContent,String campusInformationDate){	
		CampusInformationService cis = new CampusInformationService();
		session=request.getSession();
		String userId=(String) session.getAttribute("userId");
		CampusInformation ci =new CampusInformation(Integer.parseInt(userId),campusInformationTitle,campusInformationContent,campusInformationDate);			
		try {
			if(cis.Campus(ci)){
				return new ModelAndView("/index");
			}else{
				return new ModelAndView("/school-message");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	@RequestMapping ("/information")
	public ModelAndView showInfomation(HttpServletRequest request) throws Exception {
		CampusInformationService cis = null;
		try {
			cis = new CampusInformationService();
		} catch (Exception e) {
			e.printStackTrace();
		}

		List<CampusInformation> list = cis.campus2();
		request.setAttribute("information", list);
		System.out.println(list.toString());
		return new ModelAndView("/index");
	}
}